﻿/*    
Nemulation: A .NET based framework/platform for virtualization applications
Copyright (C) 2011 Bryan Perris

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using NEmu.Debugging;
using System.Diagnostics;
using System.IO;

namespace NEmu.TestUnits
{
    [TestFixture, Description("Tests the NEmu logging system")]
    public sealed class TestMachineB : VirtualMachine
    {
        TestResourceManger m_Resources;
        TestMemory m_Memory;

        public TestMachineB()
        {
            m_Memory = new TestMemory(this);
            m_Resources = new TestResourceManger(this);
        }

        protected override void Boot()
        {

        }

        protected override void InitializeComponents()
        {
            
        }

        protected override void ShutdownComponents()
        {
            
        }

        public override ResourceManager Resources
        {
            get { return m_Resources; }
        }

        public override Memory.MemoryUnit MachineMemory
        {
            get { return m_Memory; }
        }

        #region Tests

        [Test, Description("Test all the log writes")]
        public void Test_LogWrites()
        {
            EmuLog.NoticeLog("Notice log");
            EmuLog.WarningLog("Warning log");
            EmuLog.ErrorLog("Error log");
            EmuLog.InfoLog("Info log");
            EmuLog.DebugLog("Debug log");
        }

        [Test, Description("Tests log messages during run/boot mode")]
        public void Test_Run()
        {
            Run();
            Stop();
        }

        [Test, Description("Tests messages using sections")]
        public void Test_SectionalMessages()
        {
            EmuLog.AddSectionName("VIDEO");
            EmuLog.AddSectionName("SOUND");

            EmuLog.SectionLog("VIDEO", LogLevel.Info, "video has init!");
            EmuLog.SectionLog("SOUND", LogLevel.Info, "sound has init!");
            EmuLog.SectionLog("KINK", LogLevel.Info, "(shouldn't see this) kink has init!");
            EmuLog.RemoveSectionName("VIDEO");
            EmuLog.SectionLog("VIDEO", LogLevel.Info, "(shouldn't see this) video has init again!");
        }

        [Test, Description("Tests listeners added to the log system")]
        public void Test_Listeners()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter writer = new StringWriter(sb);
            TextWriterTraceListener listener = new TextWriterTraceListener(writer);
            int index = EmuLog.AddTraceListener(listener);
            EmuLog.InfoLog("1. Writing to string builder");
            EmuLog.DebugLog("2. The cat jumps over the fence");
            EmuLog.WarningLog("3. Warning the cat is fat");
            EmuLog.RemoveTraceListener(index);
            Console.WriteLine("=============String builder dump===============");
            Console.WriteLine(sb.ToString());
            listener.Close();
        }

        [Test, Description("Tests asserts")]
        public void Test_Assertion()
        {
            EmuLog.Assert(1 == 2, "1 does not equal 2!");
            EmuLog.Assert(1 == 1, "(shouldn't see this) 1 is 1");
        }

        [Test, Description("Test parameterized strings")]
        public void Test_ParamString()
        {
            string a = EmuLog.ParameterString("This cat {0} like a {1}", "stinks", "fish");
            string b = "This cat stinks like a fish";
            Assert.AreEqual(a, b);
        }

        #endregion
    }
}
